package GUI;

import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JList;

import ControlLayer.OrderCtr;
import ModelLayer.SalesOrder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OrderList extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2468590267433545690L;
	private JPanel contentPane;
	private final JScrollPane scrollPane = new JScrollPane();
	private JList lstOrder;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OrderList frame = new OrderList();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public OrderList() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 623, 413);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JButton btnVisAlle = new JButton("vis alle ");
		btnVisAlle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateOrderList();
			}
		});
		btnVisAlle.setBounds(508, 341, 97, 25);
		contentPane.add(btnVisAlle);
		scrollPane.setBounds(0, 0, 605, 341);
		contentPane.add(scrollPane);
		
		lstOrder = new JList();
		scrollPane.setViewportView(lstOrder);
		
		JButton btnNyOrder = new JButton("ny order");
		btnNyOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showOrderMaker();
			}
		});
		btnNyOrder.setBounds(409, 341, 97, 25);
		contentPane.add(btnNyOrder);
		
		JButton btnNyDelordre = new JButton("ny delordre");
		btnNyDelordre.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				makePartOrder();
			}
		});
		btnNyDelordre.setBounds(313, 341, 97, 25);
		contentPane.add(btnNyDelordre);
	}
	
	private void updateOrderList()
	{
		ArrayList<SalesOrder> list = new OrderCtr().findAllOrders();
		DefaultListModel<SalesOrder> dlist = new DefaultListModel<>();
		for(SalesOrder order: list)
			dlist.addElement(order);
		lstOrder.setModel(dlist);
	}
	
	private void showOrderMaker()
	{
		OrderMaker om = new OrderMaker();
		om.setVisible(true);
	}
	
	private void makePartOrder()
	{
		SalesOrder o = (SalesOrder)this.lstOrder.getSelectedValue();
		//JOptionPane.showConfirmDialog(null, o);
		OrderDetail od = new OrderDetail(o);
		od.setVisible(true);
		
	}
}
